#pragma once

#include <unordered_map>
#include <string>
#include <fstream>

#include "Log.hpp"

static char sep = ':';
std::string file_path = "dict.txt";
class Dict
{
private:
    void LoadDict(const std::string &path)
    {
        std::ifstream in(path);
        if (!in.is_open())
        {
            LOG(FATAL, "open %s failed!\n", path);
            exit(1);
        }

        std::string line;
        while (std::getline(in, line))
        {
            // 载入单词line及其翻译
            LOG(DEBUG, "load info: %s ,success\n", line.c_str());
            //
            auto pos = line.find(sep);
            // 没有译文则跳过
            if (pos == std::string::npos)
                continue;

            std::string key = line.substr(0, pos);
            _dict.insert(std::make_pair(key, line));
        }
        in.close();
    }

public:
    Dict()
    {
        _dict_confi_filepath = file_path;
        LoadDict(_dict_confi_filepath);
    }
    std::string translate(std::string word,bool&ok)
    {
        ok = true;
        auto iter = _dict.find(word);
        if(iter==_dict.end())
        {
            ok = false;
            return "None";
        }
        return iter->second;
    }
    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _dict_confi_filepath;
};